<?php
/**
 * User: 黄城
 * Date: 2017/4/13
 * Time: 11:13
 * description: 退款统计
 */

namespace Admin\Controller;


use Think\Model;
use Think\Page;

class StoreRefundController extends BaseController
{
    public $map; //查询条件
    public function __construct()
    {
        parent::__construct();
        $this->map = [
            'local_id' => session('userInfo')['local_id'],
        ];
    }

    /**
     * 退款统计列表
     */
    public function index()
    {
        $page = I('get.p', 1);
        $beginDate = I('get.begin_time', '', 'htmlspecialchars');
        $endDate = I('get.end_time', '', 'htmlspecialchars');
        if (!empty($beginDate) and !empty($endDate) and strtotime($beginDate) <= strtotime($endDate)){
            $bTime = strtotime($beginDate);
            $eTime = strtotime($endDate)+86400;
            $this->map['UNIX_TIMESTAMP(time)'] = [['egt', $bTime], ['lt', $eTime]];
        }else{
            $beginDate = '';
            $endDate = '';
        }
        $lists = M('statistical_refund')->where($this->map)->page($page.',15')
            ->field(['FROM_UNIXTIME(UNIX_TIMESTAMP(`time`), \'%Y-%m-%d\') `time`', 'refund_order_num', 'money'])->select();

        $count = M('statistical_refund')->where($this->map)->count();
        $PageObject = new Page($count, 15,['p'=>$page, 'beginTime' => $beginDate,'endDate'=> $endDate]);
        $show = $PageObject->show();

        $this->assign('data', ['page' => $show, 'begin_time' => $beginDate, 'end_time' => $endDate]);
        $this->assign('lists', $lists);
        $this->display('Refund/storeRefund');
    }

    /**
     * 某天退款的订单号
     */
    public function info()
    {
        $date = I('get.date', '', 'htmlspecialchars');
        $page = I('get.p', 1);
        if (empty($date)){
            echo "<script>alert('参数错误');history.go(-1);</script>";
            return false;
        }
        $this->map['UNIX_TIMESTAMP(time)'] = [['egt', strtotime($date)], ['lt', strtotime($date)+86400]];
        $lists = M('statistical_refund_orders')
            ->where($this->map)->page($page.', 20')
            ->field(['order_id', 'refund_type `type`'])->select();
        foreach ($lists as &$value){
            $type = $value['type'];
            if (empty($type)){
                $value['typeMsg'] = '';
            }else{
                $types = explode(',', $type);
                $typeMsg = '';
                foreach ($types as $v){
                    switch ((int)$v){
                        case 1 :
                            $msg = '售前退款';
                            break;
                        case 2 :
                            $msg = '售后退款';
                            break;
                        case 3 :
                            $msg = '售后退款退货';
                            break;
                        default:
                            $msg = '';
                            break;
                    }
                    $typeMsg .= (empty($typeMsg) ? $msg : "/$msg");
                }
                $value['typeMsg'] = $typeMsg;
            }
        }
        $count = M('statistical_refund_orders')
            ->where($this->map)
            ->field(['id'])->count();
        $PageObject = new Page($count, 20, ['p' => $page, 'date' => $date]);
        $show = $PageObject->show();
        $this->assign('lists', $lists);
        $this->assign('page', $show);
        $this->display('Refund/info');
    }
}